#coding=utf-8
import scrapy
import sys
reload(sys)
import urllib
import urllib2
import time
from lxml import etree
import datetime
sys.setdefaultencoding('utf8')
from scrapy.http import Request
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
import os


class BugDaily_Spider(scrapy.Spider):
    name = "BugDaily"
    # http://security.koolearn.com
    # 域账户用户名密码
    userName= "leixun"
    password= "378640336lQ"
    # 每日导出的excel下载保存地址
    pathParent = "/Users/leixun/Documents/AppBug/"
    # 登录地址
    loginUrl = "https://cas-sso.koolearn.com/login?"
    start_urls = ("http://security.koolearn.com",)
    queryString = {
        "service": "http://security.koolearn.com/security/j_spring_cas_security_check"
    }
    # bug平台分类参数 platform Android 1 ; 全部-3
    # 猜测pageSize为页大小
    bugParam = {
        "page":"1",
        "pageSize":"80",
        "appId":"",
        "appName":"",
        "userName":"",
        "content":"",
        "platform":"1",
        "replyStatus":"-1",
        "startTime":"",
        "endTime":""
    }
    startTime = ""
    # 请求bug列表地址
    bugListUrl = "http://manageapp.koolearn.com/yd/app/feedback/list?"
    # 导出excel的 下载地址
    excelUrl = "http://manageapp.koolearn.com/yd/app/feedback/export?"
    # heards默认配置
    custom_settings = {
        "RANDOM_DELAY": 3,
        "DEFAULT_REQUEST_HEADERS": {
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
            'accept-encoding': 'gzip, deflate, br',
            'Connection': 'keep - alive',
            'Refer':'http://security.koolearn.com/',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
        },
    }

    def start_requests(self):
        encodeData = urllib.urlencode(self.queryString)
        yield Request(url=self.loginUrl+ encodeData, callback=self.parse)
    #
    def parse(self,response):
        yield scrapy.FormRequest.from_response(
            response,
            formdata={
                "username": self.userName,
                "password": self.password,
                "It": "_c35F4FAF6-1632-57F1-D4E1-6E434CA73FBA_k8014A5B9-4C6B-693B-7E34-D16ECCBA8C65",
                "_eventId": "submit",
                "submit": "登录"
            },
            callback=self.jumpToBugPage)
    def jumpToBugPage(self,response):
        # 访问 app用户反馈网页地址
        yield Request(
            url="http://security.koolearn.com/ext_desktop?_method=_new_window&menuId=3762",
        callback=self.requestBugList)

    # 配置参数 请求bug列表
    def requestBugList(self,response):
        today = datetime.date.today()
        yesterday = today - datetime.timedelta(days=1)
        startTime = time.strptime(str(yesterday), '%Y-%m-%d')
        self.startTime = time.strftime('%Y-%m-%d',startTime)
        print self.startTime
        self.bugParam['startTime']=self.startTime
        self.bugParam['endTime'] =self.startTime
        url = self.bugListUrl+urllib.urlencode(self.bugParam)
        print url
        yield Request(url=url, callback=self.parseBug)
    # 根据模板 组装html内容
    def parseBug(self,response):
        html = etree.HTML(response.body)
        # itemBean['secName'] = html.xpath("//a/u/text()")[0]
        html = self.html.replace("shit",str(response.body))
        self.context = MIMEText(html, _subtype='html', _charset='utf-8')
        print html
        url = self.excelUrl + urllib.urlencode(self.bugParam)
        yield Request(url=url, callback=self.requestExcel)
    # 下载相应的excel文件 并将html作为内容 excel作为附件 发送到相应用户邮箱
    def requestExcel(self, response):
        print self.startTime
        filename = ""
        if self.bugParam['platform'] == "1":
            filename = "Android反馈"+str(self.startTime)+".xls"
        else:
            filename = "App反馈"+str(self.startTime)+".xls"
        # 开始写文件
        isExists = os.path.exists(self.pathParent)
        if not isExists:
            os.makedirs(self.pathParent)
        file_path = os.path.join(self.pathParent, filename)
        with open(file_path, "wb") as f:
            f.write(response.body)
        print "下载成功："+filename
        self.send_mail_to_test(self.context,filename,file_path)

    def send_mail(self,config):
        print 'Sending Mail...'
        message = MIMEMultipart()
        message["Accept-Charset"] = "ISO-8859-1,utf-8"
        message['From'] = 'leixun33@163.com'
        message['To'] = ','.join(config['to'])
        message['CC'] = ','.join(config['cc'])
        message['Subject'] = "Android 用户反馈"
        message['Date'] = time.ctime(time.time())
        # message['Reply-To'] = 'pingyang.wang@mail.cn'
        message['X-Priority'] = '3'
        message['X-MSMail-Priority'] = 'Normal'
        if config['text']:
            text = config['text']
            message.attach(text)
        if config['filename']:
            filepath=config['filepath']
            print filepath
            part = MIMEApplication(open(filepath, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=config['filename'])
            message.attach(part)
        smtp = smtplib.SMTP_SSL(config['server'],465)
        smtp.login(config['username'], config['password'])
        smtp.sendmail(config['username'], config['to'], message.as_string())
        print 'Send Mail OK'
        smtp.close()
        time.sleep(1)

    # password为QQ给的秘钥，并非邮箱密码
    def send_mail_to_test(self,context,filename,filePath):
        self.send_mail({
            'to': ["leixun@koolearn-inc.com","lixiangyue@koolearn-inc.com","yankeliang@koolearn-inc.com","songbinbin@koolearn-inc.com"],
            'cc': ["yankeliang@koolearn-inc.com","songbinbin@koolearn-inc.com"],
            'server': 'smtp.qq.com',
            'port': 25,
            'subject': 'Just for Test',
            'username': '378640336@qq.com',
            'password': 'aznyzgnbtjbtcaeg',
            'filename': filename,
            'filepath': filePath,
            'text': context}
        )

    # html 内容模板
    html = """\
        <!DOCTYPE html>
        <html>
            <meta charset="utf-8">
        <head>
        <style type="text/css">
            .p-thumbnail {display: inline-block;}
            .text-left {text-align: left;}
            .p-thumbnail__item{width: 50px;}
            </style>
            <link rel="stylesheet" type="text/css" href="http://static.koocdn.com/framework/css/global.17622.css">
            <link rel="stylesheet" type="text/css" href="http://static.koocdn.com/lib/bootstrap/css/bootstrap.15753.css">
            <link rel="stylesheet" type="text/css" href="http://static.koocdn.com/project/cms-mix/1.x/common/bootstrap-reset/bootstrap-reset.16583.css">
            <title>Android - 用户反馈日报</title>
        </head>
        <body>
        <div id="container">
            <div id="content">
            <p>详情见附件</p>
                <p>
                shit
                </p>
            </div>
        </div>
        </body>
        </html>
            """